import app, { xlsxCfgNames } from "../../../app";
import { att_preference, bar } from "../../../shared/interface";
import { Tpl_panel_equip } from "../../../shared/master/MsgAction";
import { _Cls_Points } from "../body/att/_Points";
import { _Cls_Att } from "../body/att/_Att";
import { SKILL } from "../skill/SK_active";
import SK_func from "../skill/SK_func";
import god from "../../../god";

/**
 * name
 * type 特殊装备还是普通装备
 * 一级属性点
 * Points
 * 二级属性
 * attributes
 * 技能组
 * skills
 */

export class equip {
    uuid: string = app.v4()
    attributes: _Cls_Att = new _Cls_Att();
    skills: SKILL[] = [];

    icon:string = '⚔️'
    leve: bar = {
        num: 0,
        now: 0,
        max: 100
    }
    tpl_id: string = '-2'
    name: string = '妖刀村雨';
    /**
     * 产出位置
     */
    from: string = '未知空间';
    /**
     * 装备评分
     */
    score: number = 0;
    type: att_preference = att_preference.纯血型
    constructor() {

    }
    strengthen_get() {
        return 100 / this.leve.num * 2 + 10
    }
    strengthen_need() {
        return Math.floor(this.leve.num * this.getScore() / 100)
    }

    strengthen_down() {
        if (this.leve.num <= 10) {
            return 0
        } else {
            return 100 / this.leve.num
        }
    }
    strengthen_start(doubling: boolean) {
        let rang = this.strengthen_get();
        if(doubling){
            rang = rang * 2;
        }
        if (rang > app.generateRandom(0, 100)) {
            this.upLeve();
            return true
        }
        if (this.strengthen_down() > app.random(0, 100)) {
            this.upLeve(-1);
        }
        return false;
    }
    getScore() {
        let score = this.score * (1 + this.leve.num / 6);
        for (let i = 0; i < this.skills.length; i++) {
            const element = this.skills[i];
            score += element.score;
        }
        return score
    }
    addExp(n: number) {
        app.leveAddExp(n, this.leve, this.upLeve);
    }
    private upLeve = (down?: number) => {
        this.leve.num += down || 1;
        let base = god.getServerCfgItem('leve_exp_base').b;
        this.leve.max = app.random(this.leve.num * this.score * base * 0.8, this.leve.num * this.score * 1.2 * base);
    }
    random(score: number) {
        this.attributes.random(score, this.type);
    }
    reload(data: equip) {
        let _data: any = data;
        // this.P.reset(data.P);
        this.attributes.reset(data.attributes)
        const jumpKey = ['attributes', 'skills']
        for (const key in data) {
            if (key.startsWith('_')) {
                continue;
            }
            if (jumpKey.includes(key)) {
                continue;
            }
            this[key] = _data[key];

        }

        for (let i = 0; i < data.skills.length; i++) {
            let e = data.skills[i];
            this.skills.push(new SKILL(e))
        }
    }
    get_panel() {
        let tpl: Tpl_panel_equip = {
            leve: this.leve,
            name: this.name,
            score: this.getScore(),
            from: this.from,
            att: this.getAttributes()
        }
        return tpl;
    }
    // getPoints() {
    //     return this.P;
    // }
    getAttributes() {
        return this.attributes.getMultiple_all(1 + 1 * this.leve.num / 6);
    }
    /**
     * 实例化出一件装备
     * 根据传入评分 生成随机属性
     */
    create_cfg(id: string, score: number, from: string) {
        this.score = score;
        this.from = from;
        this.tpl_id = id;
        const cfgMap = app.xlsxCfgMap.get(xlsxCfgNames.装备模板) as Map<string, any>;
        let tpl = cfgMap.get(id);
        const table = [...cfgMap.values()]
        if (!tpl) {
            // 随机
            const idx = app.randomWeight(table)
            tpl = table[idx]
        }
        this.icon = tpl.icon;
        this.type = tpl.type;
        this.name = tpl.name;
        this.random(score);
        let surplusScore = score;
        for (const key in tpl) {
            if (key.includes('skill_')) {
                if (surplusScore < 10) {
                    break;
                }
                if (app.random(0, 100) < 50) {
                    continue;
                }
                const skName = tpl[key] as string;
                const rename = skName.split('-')
                if (rename[0] == '') {
                    break;
                }
                let randomScore = app.generateRandom(1, surplusScore);
                let skTpl = SK_func.createSkill(rename[0], randomScore, rename[1])
                if (!skTpl) {
                    break;
                }
                this.score += randomScore;
                surplusScore -= randomScore;
                this.skills.push(skTpl)
            }
        }
        this.upLeve();
    }
}
